﻿using System.IO;
using System.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;

namespace C12_Ex03_Shaked_032675316_Itay_032511057
{
    public partial class WallPost : UserControl
    {
        private Post m_Post;
        public Action m_CommentPostedAction;

        public Action CommentPostedAction
        {
            get { return m_CommentPostedAction; }
            set { m_CommentPostedAction = value; }
        }

        public WallPost()
        {
            InitializeComponent();
        }

        private void insertPost()
        {
            WebRequest req;
            WebResponse response;
            Stream stream;
            LabelPoster.Text = m_Post.From.Name + "\t" + DateTime.Now.GetDifferenceDate(m_Post.CreatedTime.Value) + " ago";
            LabelContent.Text = m_Post.Message;

            req = WebRequest.Create(m_Post.From.PictureSqaureURL);
            response = req.GetResponse();
            stream = response.GetResponseStream();
            PicturePoster.Image = Image.FromStream(stream);

            if (m_Post.PictureURL != null)
            {
                req = WebRequest.Create(m_Post.PictureURL);
                response = req.GetResponse();
                stream = response.GetResponseStream();
                pictureBoxImagePost.Image = Image.FromStream(stream);
            }
            else
            {
                pictureBoxImagePost.Hide();
            }

            TextBoxComments.Text = string.Empty;

            if (m_Post.Comments.Count == 0)
            {
                TextBoxComments.Hide();
                LabelComments.Hide();
            }

            foreach (Comment comment in m_Post.Comments)
            {
                TextBoxComments.Text += comment.From.Name + " : \"" + comment.Message + "\"" + Environment.NewLine;
            }

            switch (m_Post.Type)
            {
                case Post.eType.link:
                    LinkLabel linkLabel = new LinkLabel();
                    linkLabel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                    linkLabel.Links.Add(0, m_Post.Link.Length, m_Post.Link);
                    break;
            }
        }

        public Post Post
        {
            get { return m_Post; }
            set
            {
                m_Post = value;
                insertPost();
            }
        }

        private void ButtonLike_Click(object sender, EventArgs e)
        {
            SetLikeButton(m_Post.Like());
        }

        private void SetLikeButton(bool isLiked)
        {
        }

        private void buttonPostComment_Click(object sender, EventArgs e)
        {
            m_Post.Comment(TextBoxPostComment.Text);
            m_CommentPostedAction();
        }
    }
}
